package org.pms.demo.webservice.sign;

import org.pms.demo.webservice.utils.StringUtils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    private final static String constants_salt = "abcd1234efg567";
    public final static String getMd5(String plaintext,String salt){
        if(StringUtils.isNullOrEmpty(salt)){
            salt="";
        }
        return getMD5(getMD5(plaintext)+getMD5(constants_salt)+getMD5(salt));
    }
    public final static String getMD5(String plaintext) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");

            md.reset();
            md.update(plaintext.getBytes());
            byte[] digest = md.digest();
            BigInteger bigInt = new BigInteger(1, digest);
            String hashtext;
            for (hashtext = bigInt.toString(16); hashtext.length() < 32; hashtext = "0" + hashtext) ;
            return hashtext;
        } catch (NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        }
        return "";
    }
    public final static boolean verify(String sign, String plaintext,String salt) {
        String mysign = getMd5(plaintext,salt);
        if (mysign.equals(sign)) {
            return true;
        }
        return false;
    }




}